import 'package:boss/models/commodity.dart';
import 'package:boss/provider/commodity_provider.dart';
import 'package:boss/ui/widget/alert/alert.dart';
import 'package:boss/ui/widget/back_confirmation.dart';
import 'package:boss/ui/widget/center_title_app_bar.dart';
import 'package:boss/ui/widget/label_button.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:router/annotation.dart';

import 'provider/package_detail_provider.dart';
import 'widget/plus_package_browser.dart';

@RouterConfig(PlusPackagePage.pageName)
class PlusPackagePage extends StatefulWidget {
  static const pageName = '/PlusPackagePage';

  @Autowired()
  final Commodity package;

  const PlusPackagePage({Key key, this.package}) : super(key: key);

  @override
  _PlusPackagePageState createState() => _PlusPackagePageState();
}

class _PlusPackagePageState extends State<PlusPackagePage> {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (_) => PackageDetailProvider(
        package: widget.package ??
            Commodity.fromJson({
              'commodityCellTime': '00:00-23:59',
              'commodityCellWeek': '0/1/2/3/4/5/6',
              'commodityType': Commodity.COMMODITY_TYPE_NORMAL,
            }),
        menus: Provider.of<CommodityProvider>(context, listen: false).packageStoreMenus,
      ),
      child: BackConfirmation(
        backConfirmationText: '套餐还未完成确认返回么？',
        child: Scaffold(
          appBar: CenterTitleAppBar(
            title: '新增套餐',
            backConfirmationText: '套餐还未完成确认返回么？',
            actions: <Widget>[
              if (widget.package != null)
                LabelButton(
                  title: '删除',
                  padding: EdgeInsets.only(left: 8, top: 2, bottom: 2),
                  onTap: () async {
                    var commodityProvider = Provider.of<CommodityProvider>(
                      context,
                      listen: false,
                    );
                    if (await commodityProvider.removePackage(widget.package.id)) {
                      Navigator.pop(context);
                    }
                  },
                ),
            ],
          ),
          body: PlusPackageBrowser(),
        ),
      ),
    );
  }
}
